﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using OracleMVC.Models;

namespace OracleMVC.Controllers
{
    public class RolesController : Controller
    {
        //
        // GET: /Roles/

        public ActionResult ALLRoles()
        {
            return View(Roles.GetAllRoles().ToList());
        }

        public ActionResult ManageRole(string roleName)
        {
            return View();
        }

        public ActionResult ALLUsers()
        {
            return View(Membership.GetAllUsers());
        }

        public ActionResult EditUser(string userName)
        {
            AuthoritiesModels a = new AuthoritiesModels();
            a.userName = userName;
            string[] roles = Roles.GetRolesForUser(userName);
            a.isAdmin = roles.Contains("省公司") ? true : false;
            a.isSanMingAdmin = roles.Contains("三明") ? true : false;
            a.isNingdeAdmin = roles.Contains("宁德") ? true : false;
            return View(a);
        }

        [HttpPost]
        public ActionResult EditUser(AuthoritiesModels model)
        {
            if (ModelState.IsValid)
            {
                string[] roles = Roles.GetRolesForUser(model.userName);
                if (model.isAdmin)
                {
                    if (!roles.Contains("省公司")) Roles.AddUserToRole(model.userName, "省公司");
                }
                else
                {
                    if (roles.Contains("省公司")) Roles.RemoveUserFromRole(model.userName, "省公司");
                }

                if (model.isSanMingAdmin)
                {
                    if (!roles.Contains("三明")) Roles.AddUserToRole(model.userName, "三明");
                }
                else
                {
                    if (roles.Contains("三明")) Roles.RemoveUserFromRole(model.userName, "三明");
                }

                if (model.isNingdeAdmin)
                {
                    if (!roles.Contains("宁德")) Roles.AddUserToRole(model.userName, "宁德");
                }
                else
                {
                    if (roles.Contains("宁德")) Roles.RemoveUserFromRole(model.userName, "宁德");
                }
            }
            return View(model);
        }



    }
}
